package com.wl156.activities.order;

import java.io.IOException;

import org.ksoap2.serialization.SoapObject;
import org.xmlpull.v1.XmlPullParserException;

import android.os.AsyncTask;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wl156.BaseActivity;
import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.utils.Utils;

public class MyOrderDetail extends BaseActivity {
	private WebServiceClient mWebServiceClient;
	private Certificate mCertificate;
	private String orderID;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		orderID = getIntent().getStringExtra("id");

		setContentView(R.layout.act_order_detail);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		new AsyncTask<Void, Void, Object>() {
			protected void onPostExecute(Object result) {
				try {
					if (result instanceof Exception) {
						Exception e = (Exception) result;
						Toast.makeText(MyOrderDetail.this, "加载失败，请稍后再试",
								Toast.LENGTH_SHORT).show();
					} else {
						SoapObject so = (SoapObject) result;

						LinearLayout ll = (LinearLayout) findViewById(R.id.root);
						for (int i = 0; i < ll.getChildCount(); i++) {
							LinearLayout child = (LinearLayout) ll
									.getChildAt(i);
							TextView tv = (TextView) child.getChildAt(1);
							tv.setText(Utils.propertyToShow(so
									.getProperty(i + 1)));
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
					Toast.makeText(MyOrderDetail.this, "加载失败，请稍后再试",
							Toast.LENGTH_SHORT).show();
				}
			}

			@Override
			protected Object doInBackground(Void... params) {
				try {
					return mWebServiceClient.company_getMyOrderView(
							mCertificate, orderID);
				} catch (IOException e) {
					e.printStackTrace();
					return e;
				} catch (XmlPullParserException e) {
					e.printStackTrace();
					return e;
				}
			}
		}.execute();

	}
}
